//
//  SecondViewController.m
//  A段
//
//  Created by 月月 on 15/4/10.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "SecondViewController.h"
#import "SecondTableViewCell.h"
#import "ListViewController.h"
#import "MyConnection.h"
#import "ContentViewController.h"
#import "WatchContentViewController.h"
#import "UIScrollView+PullLoad.h"
#import "UIImageView+WebCache.h"
@interface SecondViewController ()<UITableViewDataSource, UITableViewDelegate, PullDelegate>
{
    UITableView *_table;
}

@property (nonatomic, retain)NSMutableArray *tableArray;
//@property (nonatomic, retain)UITableView *table;
@property (nonatomic, assign)NSInteger count1;

@end

@implementation SecondViewController
- (void)dealloc
{
//    [_table release];
    [_tableArray release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tableArray = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    //自动偏移量
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.navigationItem.title = @"watch";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"更多" style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonAction:)];
    
    [self createTable];
    [self getConnect];
}

- (void)leftButtonAction:(id)sender
{
    ListViewController *list = [[[ListViewController alloc] init]autorelease];
    [self.navigationController pushViewController:list animated:YES];
}

#warning 创建tableview
- (void)createTable
{

    _table = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64 -  49) style:UITableViewStylePlain];
//    [_table headerViewForSection:1];
//    _table.tableHeaderView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)]autorelease];
    //行高
    [_table setRowHeight:150];
//    _table.sectionHeaderHeight = 300;
    _table.delegate = self;
    _table.dataSource = self;
    //注册重用池
    [_table registerClass:[SecondTableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_table];
    
    //上拉加载下拉刷新代理
    _table.pullDelegate = self;
    _table.canPullDown = YES;
    _table.canPullUp = YES;

    [_table release];
}
//tableviewheader方法
//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
//{
//    _table.tableHeaderView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200)]autorelease];
//    return _table.tableHeaderView;
//}
//
//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
//{
//    return 70;
//}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_tableArray count] ;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SecondTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    NSDictionary *dic = [self.tableArray objectAtIndex:indexPath.row];
//    NSData *dataImage = [NSData dataWithContentsOfURL:[NSURL URLWithString:[dic objectForKey:@"cover_thumb_hd"]]];
    [cell.watchPhoto sd_setImageWithURL:[dic objectForKey:@"cover_thumb_hd"]];
    cell.titleLabel.text = [dic objectForKey:@"title"];
    cell.pubdateLabel.text = [dic objectForKey:@"pubdate"];
    cell.summaryLabel.text = [dic objectForKey:@"summary"];
    //内容自动换行
    [cell.summaryLabel setNumberOfLines:40];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    //数据解析成字典
    NSDictionary *dic = [self.tableArray objectAtIndex:indexPath.row];
    //字典取链接
    NSString *webStr = [dic objectForKey:@"link_share"];
    WatchContentViewController *watchController = [[WatchContentViewController alloc] init];
    //属性传值
    watchController.webString = webStr;
    [self.navigationController pushViewController:watchController animated:YES];
    
}

//- (void)startConnect
//{
//    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
//    [dic setObject:@"1" forKey:@"page"];
//    [dic setObject:@"iphone" forKey:@"ver"];
//    [dic setObject:@"9" forKey:@"app_ver"];
//
//    //网络请求
//    __block SecondViewController *my = self;
//    [MyConnection connectWithUrl:@"http://watch-cdn.idailywatch.com/api/list/news/zh-hans" parmaters:dic finishBlock:^(NSData *data) {
//        
//    //解析数据
//    NSMutableArray *arr = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//    [my.tableArray addObjectsFromArray:arr];
//    [_table reloadData];
//    }];
//}

#pragma mark -
#pragma mark 上拉加载, 下拉刷新
-(void)scrollView:(UIScrollView *)scrollView loadWithState:(LoadState)state
{
    if (state == PullDownLoadState) {
        // 判断为下拉刷新, 2秒后执行downLoad方法
        [self getConnect];
    }else if(state == PullUpLoadState) {
        // 判断为上拉加载, 2秒后执行方法
        [self loadMore];
    }
    
}

// 下拉刷新
- (void)getConnect
{
    self.count1 = 1;
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:@"1", @"page", @"iphone", @"ver", @"9", @"app_ver", nil];
    
   __block SecondViewController *my = self;
    [MyConnection connectWithUrl:@"http://watch-cdn.idailywatch.com/api/list/news/zh-hans" parmaters:dic finishBlock:^(NSData *data) {
        my.tableArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        [my.tableArray removeLastObject];
        [_table reloadData];
//        [_table setFrame:CGRectMake(0, 54, self.view.frame.size.width, self.view.frame.size.height - 64)];
        [_table stopLoadWithState:PullDownLoadState];
    }];
    

}

// 上拉加载
- (void)loadMore
{
    self.count1++;
    NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[NSString stringWithFormat:@"%ld", self.count1], @"page", @"iphone", @"ver", @"9", @"app_ver", nil];
    __block SecondViewController *my = self;
    [MyConnection connectWithUrl:@"http://watch-cdn.idailywatch.com/api/list/news/zh-hans" parmaters:dic finishBlock:^(NSData *data) {
        NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        [my.tableArray addObjectsFromArray:array];
        [my.tableArray removeLastObject];
        [_table reloadData];
        
         [_table stopLoadWithState:PullUpLoadState];
    }];
   
}


@end
